package gui;

import gestionBDD.extractionBDD;
import gestionBDD.requetesTirage;
import interne.Tirage;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.trolltech.qt.gui.*;

public class fenetreTirage extends QWidget {
	
	private QListWidget tirageListWidget ;
	
	public fenetreTirage() throws SQLException {
		
		tirageListWidget = new QListWidget();
		
        /** Login's GroupBox' **/
        QGroupBox tiragesGroup = new QGroupBox(tr("Tirages disponibles"));
        QHBoxLayout albumsLayout = new QHBoxLayout();
        

        albumsLayout.addWidget(tirageListWidget);
        tiragesGroup.setLayout(albumsLayout);
        

        QGroupBox actionsGroup = new QGroupBox(tr("Actions"));

        /* Bouton Connexion */
        QPushButton boutonEditer = new QPushButton("Editer...");
        boutonEditer.clicked.connect(this, "editerTirage()"); 
        
        /* Bouton Connexion */
        QPushButton boutonAjouter = new QPushButton("Ajouter...");
        boutonAjouter.clicked.connect(this, "ajouterTirage()");    
        
        /* Bouton Connexion */
        QPushButton boutonRetirer = new QPushButton("Retirer...");
        boutonRetirer.clicked.connect(this, "retirerTirage()");   
 
        /* Bouton Connexion */
        QPushButton boutonRefresh = new QPushButton("Rafraichir");
        boutonRefresh.clicked.connect(this, "reactualiser()");  
        
        /* Bouton Retour */
        QPushButton boutonRetour = new QPushButton("Retour");
        boutonRetour.clicked.connect(this, "retour()");  

        QVBoxLayout actionsLayout = new QVBoxLayout();
        actionsLayout.addWidget(boutonEditer);
        actionsLayout.addWidget(boutonAjouter);
        actionsLayout.addWidget(boutonRetirer);
        actionsLayout.addWidget(boutonRefresh);
        actionsLayout.addWidget(boutonRetour);
        actionsGroup.setLayout(actionsLayout);

     
        QVBoxLayout layout = new QVBoxLayout();
        layout.addWidget(tiragesGroup);
        layout.addWidget(actionsGroup);
        setLayout(layout);
        
		List<Tirage> listeAlbum = new ArrayList<Tirage>(); 
		listeAlbum = extractionBDD.extraireTirages();
		Iterator<Tirage> ite = listeAlbum.iterator();
		while (ite.hasNext()) {
			Tirage tirage = ite.next();
		    tirageListWidget.addItem(tirage.getIdFormat() + " / " + tirage.getLibelle() + " = " + tirage.getPrixUnitaire()) ;
		}
	}
	
	public void editerTirage() throws SQLException {
		String temp[] = tirageListWidget.currentItem().text().split(" / ") ;
		Integer idFormat = Integer.parseInt(temp[0]) ;
		Tirage tirage = new Tirage(idFormat) ;
		tirage = requetesTirage.trouverAvecId(tirage) ;
		fenetreTirageEdition fenetre = new fenetreTirageEdition(tirage);
		fenetre.show();
	}
	
	public void ajouterTirage() throws SQLException {
		fenetreAjoutTirage fenetre = new fenetreAjoutTirage() ;
		fenetre.show() ;
	}
	
	public void retirerTirage() throws SQLException {
		String temp[] = tirageListWidget.currentItem().text().split(" / ") ;
		Integer idFormat = Integer.parseInt(temp[0]) ;
		Tirage tirage = new Tirage(idFormat) ;
		requetesTirage.retirerTirage(tirage);
		reactualiser() ;
	}
	
	public void reactualiser() throws SQLException {
		fenetreTirage fenetre = new fenetreTirage();
		QMainWindow menuPrincipal = (QMainWindow) this.parentWidget() ;
		menuPrincipal.setCentralWidget(fenetre) ;
	}
	
	public void retour() throws SQLException {
        fenetreAdmin fenetre = new fenetreAdmin();
		QMainWindow menuPrincipal = (QMainWindow) this.parentWidget() ;
		menuPrincipal.setCentralWidget(fenetre) ;
	}

}
